<?php
class ControllerCategoryProduct extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->load->model("category/product");
		$this->getList();
		$this->id='content';
		$this->template="category/product_list.tpl";
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function search()
	{
		$this->load->model("category/product");
		$this->load->model("voucher/ordertemp");
		$this->data['orderid'] = $this->request->get['orderid'];
		$order = $this->model_voucher_ordertemp->getItem($this->data['orderid']);
		
		$this->getList();
		
		$this->id='content';

		if($order['ordertype'] == "IMS" || $order['ordertype'] == "EXS")
		{
			$this->template="category/product_search.tpl";
		}
		else
		{
			$this->template="category/product_search_export.tpl";	
		}
		
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function insert()
	{
		$this->load->model("category/product");
		$this->load->model("category/productgroup");
		
		$this->data['formtype'] = 1;
		if($this->request->post['submitproduct'] == "<?php echo $this->document->text['button_save']?>")
		{
			
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
				$this->model_category_product->insert($this->request->post);
				$this->redirect($this->url->http('category/product'));
			}
		}
		elseif($this->request->post['submitgroup'] == "Save Group")
		{
			$this->data['formtype'] = 2;
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateQuickAdd())) {
				$group = array("groupname" => $this->request->post['quick_groupname'], "brandid" => $this->request->post['quick_brandid']);
				$this->request->post['groupid'] = $this->model_category_productgroup->insert($group);
				$this->data['formtype'] = 1;
			}
		}
		
    	$this->getForm();
	}
	
	public function update() {
		$this->load->model("category/product");
		
		$this->data['formtype'] = 1;
		if($this->request->post['submitproduct'] == "<?php echo $this->document->text['button_save']?>")
		{
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
				$this->request->post['productid'] = $this->request->get['productid'];
				$this->model_category_product->update($this->request->post);
				$this->session->data['success'] = $this->language->get('text_success');
				$this->redirect($this->url->http('category/product'));
			}
		}
		elseif($this->request->post['submitgroup'] == "Save Group")
		{
			$this->data['formtype'] = 2;
			if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateQuickAdd())) {
				$group = array("groupname" => $this->request->post['quick_groupname'], "brandid" => $this->request->post['quick_brandid']);
				$this->request->post['groupid'] = $this->model_category_productgroup->insert($group);
				$this->data['formtype'] = 1;
			}
		}
    	
    
    	$this->getForm();
  	}
	
	public function delete() {
		//$this->load->language('core/site');
		//$this->data = array_merge($this->data, $this->language->getData());
		
		//$this->document->title = $this->language->get('heading_title');
		$this->load->model("category/product");
			
    	if (isset($this->request->post['delete'])) 
		{
			foreach ($this->request->post['delete'] as $productid) {
				$this->model_category_product->delete($productid);
			}
			$this->data['output'] = "Xóa thành công";
    	}
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	public function newbarcode() {
		//$this->load->language('core/site');
		//$this->data = array_merge($this->data, $this->language->getData());
		
		//$this->document->title = $this->language->get('heading_title');
		$this->load->model("category/product");
		
		$prefix = "P".date("Y").date("m");
		$this->data['output'] = $this->model_category_product->nextbarcodeID($prefix);
    	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('category/product/insert');
		$this->data['delete'] = $this->url->http('category/product/delete');		
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		
		
		
		
		$this->load->model("category/productgroup");
		$this->load->model("common/control");
		
		
		$groups = $this->model_category_productgroup->getList();
		for($i = count($groups) - 1; $i >= 0; $i--)
		{
			$groups[$i]['groupname2'] =  $groups[$i]['groupname'] . "(".$groups[$i]['brandname'].")";
			$groups[$i+1] = $groups[$i];
		}
		
		$groups[0]['groupid'] =  "%";
		$groups[0]['groupname2'] =  "All";
		
		
		
		$datafields = array(
			0 => array("datafield" => "productname", "datadisplay" => "Product Name"),
			1 => array("datafield" => "barcode", "datadisplay" => "Barcode"),
			2 => array("datafield" => "line1", "datadisplay" => "Line1")
		);
		
		$numberfields = array(
			0 => array("numberfield" => "saleprice", "numberdisplay" => "Selling Price"),
			1 => array("numberfield" => "netprice", "numberdisplay" => "Net Price")
		);
		
		$functions = array(
			0 => array("functionid" => "equal", "functiondisplay" => "Equal"),
			1 => array("functionid" => "greaterthan", "functiondisplay" => "Greater than"),
			2 => array("functionid" => "lessthan", "functiondisplay" => "Less than"),
			3 => array("functionid" => "notequal", "functiondisplay" => "Not equal")
		);
		
		$this->data['search_productgroup'] = trim($this->request->post['search_productgroup']);
		$this->data['search_datafield'] = trim($this->request->post['search_datafield']);
		$this->data['search_datavalue'] = trim($this->request->post['search_datavalue']);
		$this->data['search_numberfield'] = $this->request->post['search_numberfield'];
		$this->data['search_function'] = $this->request->post['search_function'];
		$this->data['search_numbervalue'] = trim($this->request->post['search_numbervalue']);
		
		
		$this->data['cb_productgroup'] = $this->model_common_control->getDataCombobox($groups, "groupname2", "groupid", $this->data['search_productgroup']);
		
		$this->data['cb_datafield'] = $this->model_common_control->getDataCombobox($datafields, "datadisplay", "datafield", $this->data['search_datafield']);
		
		$this->data['cb_numberfield'] = $this->model_common_control->getDataCombobox($numberfields, "numberdisplay", "numberfield", $this->data['search_numberfield']);
		
		$this->data['cb_function'] = $this->model_common_control->getDataCombobox($functions, "functiondisplay", "functionid", $this->data['search_function']);
		
		
		
		
		//Get data list
		$this->data['products'] = array();
		
		if($this->data['search_productgroup'] != "")
		{
			$where = " AND `product`.groupid like '".$this->data['search_productgroup']."'";
		}
		
		if($this->data['search_datavalue'] != "")
		{
			$where .= " AND ".$this->data['search_datafield'] . " like '%".$this->data['search_datavalue']."%'";
		}
		
		if($this->data['search_numbervalue'] != "")
		{
			$numbervalue = (float)$this->data['search_numbervalue'];
			
			$f = " = ";
			switch($this->data['search_function'])
			{
				case "equal":
					$f = " = ";
				break;
				case "greaterthan":
					$f = " >= ";
				break;
				case "lessthan":
					$f = " <= ";
				break;
				case "notequal":
					$f = " != ";
				break;
			}
			
			$where .= " AND ".$this->data['search_numberfield'] . $f ." '".$numbervalue."'";	
		}
		
		
		
		
		$this->data['products'] = $this->model_category_product->getList($where);
		
		for($i=0; $i <= count($this->data['products'])-1 ; $i++)
		{
			$this->data['products'][$i]['link_edit'] = $this->url->http('category/product/update&productid='.$this->data['products'][$i]['productid']);
			$this->data['products'][$i]['text_edit'] = $this->document->text['button_edit'];
		}
		
		$this->load->model("category/stock");
		$this->load->model("common/control");
		$stocks = $this->model_category_stock->getList();
		
		$this->data['cb_stocks'] = $this->model_common_control->getDataCombobox($stocks, "stockname", "stockid", "");
		
	}
	
	
	private function getForm()
	{
		$this->load->helper('image');
		$this->data['error'] = @$this->error;
		
		if (!isset($this->request->get['productid'])) {
			$this->data['action'] = $this->url->http('category/product/insert');
		} else {
			$this->data['action'] = $this->url->http('category/product/update&productid=' . $this->request->get['productid']);
		}
		
		$this->data['cancel'] = $this->url->https('category/product');
	
		if ((isset($this->request->get['productid'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$this->data['product'] = $this->model_category_product->getItem($this->request->get['productid']);
    	}
		else
		{
			$this->data['product'] = $this->request->post;
		}
		
		//get Form
		if($this->data['product']['imagepath'] != "")
		{
			$this->data['imagethumbnail'] = HelperImage::resizePNG($this->data['product']['imagepath'], 200, 200);
		}
		
		$this->load->model("category/productgroup");
		$this->load->model("category/productbrand");
		$this->load->model("common/control");
		$groups = $this->model_category_productgroup->getList();
		$brands = $this->model_category_productbrand->getList();
		
		$units = array(
			0 => array("unit" => "pcs"),
			1 => array("unit" => "barrel"),
			2 => array("unit" => "bottom"),
			3 => array("unit" => "package")
		);
		
		$this->data['cb_unit'] = $this->model_common_control->getDataCombobox($units, "unit", "unit", $this->data['product']['unit']);
		$this->data['cb_productgroup'] = $this->model_common_control->getDataCombobox($groups, "groupname", "groupid", $this->data['product']['groupid']);
		
		$this->data['cbxbrand'] = $this->model_common_control->getDataCombobox($brands, "brandname", "brandid", $this->data['group']['brandid']);
		
		$this->data['DIR_UPLOADPHOTO'] = HTTP_SERVER."index.php?route=common/uploadpreview";
		
		$this->id='content';
		$this->template='category/product_form.tpl';
		$this->layout="layout/function";
		
		$this->render();
	}
	
	private function validateForm()
	{	
		if (strlen($this->request->post['productname']) == 0) {
      		$this->error['productname'] = "product name cannot be null";
    	}
		
		//Validate barcode
		if (strlen($this->request->post['barcode']) == 0) {
      		$this->error['barcode'] = "barcode cannot be null";
    	}
		else
		{
			$this->load->model("category/product");
			$barcode = $this->request->post['barcode'];
			$oldbarcode = $this->request->post['oldbarcode'];
			if($this->model_category_product->validatebarcode($barcode, $oldbarcode) == false)
			{
				$this->error['barcode'] = "barcode is dupplicated";
			}
		}
		

		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	private function validateQuickAdd()
	{	
		if (strlen($this->request->post['quick_groupname']) == 0) {
      		$this->error['quick_groupname'] = "Group name cannot be null";
    	}

		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	
	public function insertimport()
	{
		$this->load->model("voucher/orderdetailtemp");
		$this->load->model("category/product");
		
			
    	if (isset($this->request->post['select'])) 
		{
			foreach ($this->request->post['select'] as $productid) {
				$product = $this->model_category_product->getItem($productid);
				$orderid = $this->request->get['orderid'];
				
				$data['orderid'] = $orderid;
				$data['orderdetailno'] = "";
				$data['productid'] = $product['productid'];
				$data['productname'] = $product['productname'];
				$data['stockid'] = "";
				$data['qty'] = 1;
				$data['unit'] = $product['unit'];
				$data['netprice'] = $product['netprice'];
				$data['saleprice'] = $product['saleprice'];
				$data['orderprice'] = $product['netprice'];
				$data['subtotal'] = (float)$product['netprice'];
				$data['tax'] = 0;
				$data['taxamount'] = 0;
				$data['grandtotal'] = (float)$product['netprice'];
				$data['discount'] = 0;
				$data['discountamount'] = 0;
				$data['note'] = "";
				$data['expireddate'] = "";
				$data['warranty'] = "";
				
				$this->model_voucher_orderdetailtemp->insert($data);
				
			}
			$this->data['output'] = "Update successfully";
    	}
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	
	public function insertexport()
	{
		$this->load->model("voucher/orderdetailtemp");
		$this->load->model("category/product");
		
			
    	if (isset($this->request->post['select'])) 
		{
			foreach ($this->request->post['select'] as $productid) {
				$product = $this->model_category_product->getItem($productid);
				$orderid = $this->request->get['orderid'];
				
				$data['orderid'] = $orderid;
				$data['orderdetailno'] = "";
				$data['productid'] = $product['productid'];
				$data['productname'] = $product['productname'];
				$data['stockid'] = "";
				$data['qty'] = 1;
				$data['unit'] = $product['unit'];
				$data['netprice'] = $product['netprice'];
				$data['saleprice'] = $product['saleprice'];
				$data['orderprice'] = $product['saleprice'];
				$data['subtotal'] = (float)$product['netprice'];
				$data['tax'] = 0;
				$data['taxamount'] = 0;
				$data['grandtotal'] = (float)$product['netprice'];
				$data['discount'] = 0;
				$data['discountamount'] = 0;
				$data['note'] = "";
				$data['expireddate'] = "";
				$data['warranty'] = "";
				
				$this->model_voucher_orderdetailtemp->insert($data);
				
			}
			$this->data['output'] = "Update successfully";
    	}
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
}
?>